{include file="header.htm" /}
<body class="bodystyle" style="overflow-y: scroll; cursor: default; -moz-user-select: inherit;">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page min-hg-c-20">
    {include file="bar.htm" /}
    <form class="form-horizontal" id="postForm" action="{:weapp_url('Coupons/Coupons/manual_grant')}" method="post">
        <div class="ncap-form-default">
            <dl class="row">
                <dt class="tit">
                    <label for="coupon_id"><em>*</em>优惠券选择</label>
                </dt>
                <dd class="opt">
                    <a href="JavaScript:void(0);" onclick="add_coupon();" class="ncap-btn ncap-btn-green">添加</a> <span
                        id="coupon_content"></span>
                    <input type="hidden" name="coupon_id" id="coupon_id">
                </dd>
            </dl>
            <script type="text/javascript">
                function add_coupon() {
                    var url = "{:weapp_url('Coupons/Coupons/select_coupon')}";
                    //iframe窗
                    layer.open({
                        type: 2,
                        title: '请选择优惠券',
                        shadeClose: false,
                        shade: layer_shade,
                        closeBtn: 3,
                        area: ['80%', '90%'],
                        content: url
                    });
                }
            </script>
            <dl class="row">
                <dt class="tit">
                    <label for="num"><em>*</em>每人发送数</label>
                </dt>
                <dd class="opt">
                    <input type="text" autocomplete="off" name="num" value="1" id="num" class="input-txt" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');" style="width: 80px !important;text-align: right;">&nbsp;张
                    <p class="notic">如果剩余张数不足以发放给选定会员数量，则无法发放</p>
                </dd>
            </dl>
    
            <dl class="row">
                <dt class="tit">
                    <label for="type">发送类型</label>
                </dt>
                <dd class="opt">
                    <label><input type="radio" name="type" value="1" checked="checked" />&nbsp;指定用户发送</label>
                    &nbsp; &nbsp;
                    <label><input type="radio" name="type" value="2"/>&nbsp;按用户等级发送</label>
                    &nbsp; &nbsp;
                    <label><input type="radio" name="type" value="3"/>&nbsp;全部发送</label>
                    <p class="notic"></p>
                </dd>
            </dl>
    
            <dl class="row" id="cate1">
                <dt class="tit">
                    <label for="users_id"><em>*</em>指定用户</label>
                </dt>
                <dd class="opt">
                    <input type="text" name="users_id" id="users_id" value="" class="input-txt" placeholder="请用半角逗号隔开用户的ID,如:1,2,3">
                    <p class="notic">可指定多位用户</p>
                </dd>
            </dl>
    
            <dl class="row none" id="cate2">
                <dt class="tit">
                    <label><em>*</em>用户等级</label>
                </dt>
                <dd class="opt">
                    {foreach name="$users_level" item="vo" key="k" }
                    <label><input type="checkbox" name="level_id[]" value="{$vo.level_id}">{$vo.level_name}</label>&nbsp;&nbsp;
                    {/foreach}
                    <span class="err"></span>
                    <p class="notic">选中的会员级别才有资格领取该优惠券</p>
                </dd>
            </dl>
        </div>
        <div class="ncap-form-default">
            <div class="bot">
                <a href="JavaScript:void(0);" onclick="checkForm();" class="ncap-btn-big ncap-btn-green" id="submitBtn">保存</a>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    $(function () {
        $('input[name=type]').click(function() {
            if (1 == $(this).val()) {
                $('#cate1').show();
                $('#cate2').hide();
            } else if (2 == $(this).val()) {
                $('#cate1').hide();
                $('#cate2').show();
            }else{
                $('#cate1').hide();
                $('#cate2').hide();
            }
        });
    });

    function checkForm(){
        if ($('#coupon_id').val() == '') {
            showErrorMsg('请先选择优惠券');
            return false;
        }
        if ($('#num').val() == '' || 1 >$('#num').val()) {
            showErrorMsg('请填写有效数量');
            $('#num').focus();
            return false;
        }
        if (1 == $("input[name='type']:checked").val()){

            if ($('#users_id').val() == ''){
                showErrorMsg('请填写指定用户');
                $("input[name='users_id']").focus();
                return false;
            }
        }else if (2 ==  $("input[name='type']:checked").val()) {
            var level_id_checked = 0;

            $("input[name='level_id[]']:checked").each(function () {
                level_id_checked = $(this).val();
            })
            if (0 == level_id_checked){
                showErrorMsg('请选择用户等级');
                $("input[name='level_id']").focus();
                return false;
            }
        }

        layer_loading('正在处理');
        $.ajax({
            type : 'post',
            url : "{:weapp_url('Coupons/manual_grant', ['_ajax'=>1])}",
            data : $('#postForm').serialize(),
            dataType : 'json',
            success : function(res){
                layer.closeAll();
                if(res.code == 1){
                    layer.msg(res.msg, {icon: 1, shade: 0.1, time: 1000}, function(){
                        window.location.reload();
                    });
                }else{
                    showErrorMsg(res.msg);
                }
            },
            error: function(e){
                layer.closeAll();
                showErrorAlert(e.responseText);
            }
        });
    }
</script>
{include file="footer.htm" /}